<template>
	<view class="search">
		<musicHead title="搜索" :icon="true" :iconblack="true"/>
		<view class="container">
			<scroll-view scroll-y="true" >
				<view class="searchSearch">
					<text class="iconfont iconsearch"/>
					<input type="text" placeholder="搜索歌曲" v-model="searchWord" @confirm="handleToSearch(searchWord)" @input="handleToSuggest"/>
					<text class="iconfont iconguanbi" v-show="searchType !==1" @tap="handleToClose"/>
				</view>
				<block v-if="searchType === 1 ">
				<view class="searchHistory">
					<view class="searchHistoryHead">
						<text>历史记录</text>
						<text class="iconfont iconlajitong" @tap="handleToClear"></text>
					</view>
					<view class="searchHistoryList">
						<view v-for="(i,index) in searchHistory" :key="index" @tap="handleToWord(i)">
							{{i}}
						</view>
					</view>
				</view>
				<view class="searchHot">
					<view class="searchHotHead">
						热搜榜
					</view>
					<view class="searchHotItem" v-for="(i,index) in searchHot" :key="index" @tap="handleToWord(i.searchWord)">
						<view class="searchHotTop">
							{{index+1}}
						</view>
						<view class="searchHotWord">
							<view>
								{{i.searchWord}}
								<image :src="i.iconUrl" mode="aspectFit"></image>
							</view>
							<view>
								{{i.content}}
							</view>
						</view>
						<view class="searchHotCount">
							{{$filters.formatCount(i.score)}}
						</view>
					</view>
				</view>
				</block>
				<block v-else-if="searchType === 2">
					<view class="searchResult">
						<view class="searchResultItem" v-for="(i,index) in searchList" :key="index" @tap="handleToDetail(i.id)">
							<view class="searchResultWord">
								<view>
									{{i.name}}
								</view>
								<view>
									<image src="http://localhost:5173/static/dujia.png" mode=""></image>
									{{i.artists[0].name}}---{{i.album.name}}
								</view>
							</view>
							<text class="iconfont iconbofang"></text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType===3">
					<view class="searchSuggest">
						<view class="searchSuggestHead">
							搜索“{{searchWord}}”
						</view>
						<view class="searchSuggestItem" v-for="(i,index) in searchSuggest" :key="index" @tap="handleToWord(i.keyword)">
							<text class="iconfont iconsearch"></text>
							{{i.keyword}}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script setup lang="ts">
import {onLoad} from '@dcloudio/uni-app'
import {ref} from 'vue'
import musicHead from '../../components/musicHead/musicHead.vue'
import {getSearchHotDetail,getSearchWordDetail,getSearchSuggestDetail} from '../../services/api'

interface searchHotItem{
	searchWord:string,
	content:string,
	iconUrl:string,
	score:number
}
const searchHot = ref<searchHotItem[]>([])
const searchWord = ref<string>('')
const searchHistory = ref<any[]>([])
const searchType = ref(1)
const searchList = ref<any[]>([])
const searchSuggest = ref<any[]>([])

const handleToWord = (word:string) => {
	searchWord.value = word
	handleToSearch(word)
}
const handleToSearch = (word:string) => {
	searchHistory.value.unshift(word)
	searchHistory.value = [...new Set(searchHistory.value)]
	if(searchHistory.value.length > 10){
		searchHistory.value.length = 10
	}
	wx.setStorage({
		key:'searchHistory',
		data:searchHistory.value
	})
	getSearchList(word)
}
const handleToClear = () => {
	wx.clearStorage({
		key:'searchHistory',
		success:()=>{
			searchHistory.value = []
		}
	})
}
const getSearchList = (word:string) => {
	getSearchWordDetail(word).then((res:any)=>{
		if(res.data.code===200){
			searchList.value = res.data.result.songs
			searchType.value = 2
		}
	})
}
const handleToClose = () => {
	searchWord.value =''
	searchType.value = 1
}
const handleToDetail = (id:number) => {
	uni.navigateTo({
		url:`/pages/detail/detail?id=${id}`
	})
}
const handleToSuggest = (e:any) => {
	const value = e.detail.value
	if(!value){
		searchType.value = 1
		return
	}
	getSearchSuggestDetail(value).then((res:any)=>{
		if(res.data.code === 200){
			searchSuggest.value = res.data.result.allMatch
			searchType.value = 3
		}
	})
}

interface searchHotResult{
	cookies:[],
	data:{
		code:number,
		data:[searchHotItem]
	},
	errMsg:string,
	header:{},
	statusCode:number
}
onLoad(()=>{
	getSearchHotDetail().then((res:searchHotResult)=>{
		if(res.data.code===200){
			searchHot.value = res.data.data
		}
	})
	wx.getStorage({
		key:'searchHistory',
		success: (res) => {
			searchHistory.value = res.data
		}
	})
})
</script>

<style scoped lang='scss'>
.searchSearch{
	display: flex;
	align-items: center;
	height: 70rpx;
	margin: 70rpx 30rpx 50rpx 30rpx;
	background-color: #f7f7f7;
	border-radius: 50rpx;
	text{
		margin: 0 26rpx;
	}
	input{
		flex: 1;
		font-size: 26rpx;
	}
}
.searchHistory{
	margin: 0 30rpx 50rpx 30rpx;
	font-size: 26rpx;
	.searchHistoryHead{
		display: flex;
		justify-content: space-between;
		margin-bottom: 36rpx;
		
	}
	.searchHistoryList{
		display: flex;
		flex-wrap: wrap;
		view{
			margin-right: 30rpx;
			margin-bottom: 30rpx;
			padding: 16rpx 28rpx;
			border-radius: 40rpx;
			background-color: #f7f7f7;
		}
	}
}
.searchHot{
	margin: 0 30rpx;
	font-size: 26rpx;
	.searchHotHead{
		margin-bottom: 36rpx;
	}
	.searchHotItem{
		display: flex;
		align-items: center;
		margin-bottom: 58rpx;
	}
	.searchHotTop{
		color: #fb2222;
		width: 60rpx;
		margin-left: 8rpx;
	}
	.searchHotWord{
		flex: 1;
		view:nth-child(1){
			font-size: 30rpx;
			color: black;
		}
		view:nth-child(2){
			font-size: 24rpx;
			color: #878787;
		}
		image{
			width: 48rpx;
			height: 22rpx;
		}
	}
	.searchHotCount{
		color: #878787;
	}
}
.searchResult{
	border-top: 2rpx solid #e4e4e4;
	padding: 30rpx;
	.searchResultItem{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 30rpx;
		margin-bottom: 30rpx;
		border-bottom: 2rpx solid #e4e4e4;
		text{
			font-size: 50rpx;
		}
	}
	.searchResultWord{
		view:nth-child(1){
			margin-bottom: 12rpx;
			font-size: 28rpx;
			color: #235790;
		}
		view:nth-child(2){
			display: flex;
			font-size: 22rpx;
			color: #898989;
			image{
				margin-top: 8rpx;
				margin-right: 10rpx;
				width: 32rpx;
				height: 20rpx;
			}
		}
	}
}
.searchSuggest{
	border-top: 2rpx solid #e4e4e4;
	padding: 30rpx;
	font-style: 26rpx;
	.searchSuggestHead{
		margin-bottom: 74rpx;
		color: #457a45;
	}
	.searchSuggestItem{
		margin-bottom: 74rpx;
		color: #5d5d5d;
		text{
			margin-right: 28rpx;
			margin-top: 2rpx;
			color: #bdbdbd;
		}
	}
}
</style>